import React from 'react'
import { Button, Checkbox, Form, Input } from 'antd';
import instance from '../../api/request';
import { useNavigate } from 'react-router-dom';
import { useDispatch } from 'react-redux';
import { Tookle } from '../../store/city/Index';
import { User } from '../../store/city/Index';
function Index() {
  let navigate=useNavigate()
  let Dispatch=useDispatch()
  const onFinish = values => {
  console.log('Success:', values);
  instance.post('/login',values).then((res)=>{
    if(res.status==200){
      localStorage.setItem('token',res.data.token)
      localStorage.setItem('username',res.data.username)
      Dispatch(Tookle(res.data.token))
         Dispatch(User(res.data.username))
      alert('登录成功')
      navigate('/')
    }else{
      alert('登录失败')
    }
  })
};
const onFinishFailed = errorInfo => {
  console.log('Failed:', errorInfo);
};
  return (
    <div>
      <div style={{marginTop:"200px",width:"800px",marginLeft:"400px"}}>
        <h2 style={{textAlign:"center"}}>react-ant-admin</h2>
        
        <h4 style={{textAlign:"center"}}>欢迎登录,请先登录</h4>
      
  <Form
    name="basic"
    labelCol={{ span: 8 }}
    wrapperCol={{ span: 16 }}
    style={{ maxWidth: 600 }}
    initialValues={{ remember: true }}
    onFinish={onFinish}
    onFinishFailed={onFinishFailed}
    autoComplete="off"
  >
    <Form.Item
      label="账号"
      name="account"
      rules={[{ required: true, message: '请输入正确的账号！' ,
       
       }]}
    >
      <Input />
    </Form.Item>

    <Form.Item
      label="密码"
      name="password"
      rules={[{ required: true,  message: '请输入正确的密码！' ,
        // 15.对登录账号密码进行非空验证，
        rules: /^w{6,12}$/ // 密码需 6 - 12 位数字和字母组成。
       }]}
    >
      <Input.Password />
    </Form.Item>

    <Form.Item name="remember" valuePropName="checked" label={null}>
      <Checkbox>记住我</Checkbox>
    </Form.Item>

    <Form.Item label={null}>
      <Button type="primary" htmlType="submit">
        登录
      </Button>
       <Button  style={{left:"210px"}}>
        重置
      </Button>
    </Form.Item>
  </Form>
      </div>
    
    </div>
  )
}

export default Index